<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f5f5f5;
        }

        .login-container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .login-container h2 {
            margin: 0 0 20px;
        }

        .login-container input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        .login-container button {
            width: 100%;
            padding: 10px;
            background-color: #28a745;
            border: none;
            color: white;
            border-radius: 4px;
            cursor: pointer;
        }

        .login-container button:hover {
            background-color: #218838;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2>登录</h2>
    <input type="text" id="username" placeholder="用户名">
    <input type="password" id="password" placeholder="密码">
    <button onclick="login()">登录</button>
</div>
<script>
    function login() {
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        fetch("/admin/token", {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({username, password})
        }).then(response => {
            if (response.ok) {
                return response.json();
            } else {
                throw new Error('网络错误');
            }
        }).then(data => {
            if (data.success) {
                window.location.href = 'index.html';
            } else {
                alert('用户名或密码错误');
            }
        }).catch(error => {
            console.error('登录失败:', error);
            alert('登录失败，请检查网络或输入');
        });
    }

</script>
</body>
</html>
